﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Animations;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Media;
using Windows.UI;

namespace LFMTest
{
    public sealed partial class Profile
    {
        lfm.Artist _artist = new lfm.Artist();

        public Profile()
        {
            InitializeComponent();
            //LayoutRoot.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            pRing.IsActive = true;
            LoadArtist("Take That");
        }

        #region state management

        // View state management for switching among Full, Fill, Snapped, and Portrait states

        private DisplayPropertiesEventHandler _displayHandler;
        private TypedEventHandler<ApplicationLayout, ApplicationLayoutChangedEventArgs> _layoutHandler;

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (_displayHandler == null)
            {
                _displayHandler = Page_OrientationChanged;
                _layoutHandler = Page_LayoutChanged;
            }
            DisplayProperties.OrientationChanged += _displayHandler;
            ApplicationLayout.GetForCurrentView().LayoutChanged += _layoutHandler;
            SetCurrentOrientation(this);
        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            DisplayProperties.OrientationChanged -= _displayHandler;
            ApplicationLayout.GetForCurrentView().LayoutChanged -= _layoutHandler;
        }

        private void Page_LayoutChanged(object sender, ApplicationLayoutChangedEventArgs e)
        {
            SetCurrentOrientation(this);
        }

        private void Page_OrientationChanged(object sender)
        {
            SetCurrentOrientation(this);
        }

        private void SetCurrentOrientation(Control viewStateAwareControl)
        {
            VisualStateManager.GoToState(viewStateAwareControl, this.GetViewState(), false);
        }

        private String GetViewState()
        {
            var orientation = DisplayProperties.CurrentOrientation;
            if (orientation == DisplayOrientations.Portrait ||
                orientation == DisplayOrientations.PortraitFlipped) return "Portrait";
            var layout = ApplicationLayout.Value;
            if (layout == ApplicationLayoutState.Filled) return "Fill";
            if (layout == ApplicationLayoutState.Snapped) return "Snapped";
            return "Full";
        }

        #endregion

        private async void menu_Tapped(object sender, Windows.UI.Xaml.Input.TappedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            Windows.UI.Popups.MessageDialog m = new Windows.UI.Popups.MessageDialog("Pressed " + tb.Text);
            await m.ShowAsync();
        }

        private async void LoadArtist(String e)
        {
            
            tbk_InCollection.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            _artist = await lfm.EndPoints.Artist.getInfo(e, string.Empty, true);
            tbk_ArtistName.Text = _artist.Name;
            LayoutRoot.DataContext = _artist;
            scrollBio.MaxHeight = summaryScroll.ActualHeight;
            ParseBiography();

            
            //await Task.Run(() => Thread.Sleep(200))

            pRing.IsActive = false;
            //LayoutRoot.Visibility = Windows.UI.Xaml.Visibility.Visible;

            if (await Helpers.CheckArtistInCollection(_artist.Name))
            {
                tbk_InCollection.Visibility = Windows.UI.Xaml.Visibility.Visible;
                BackEase b = new BackEase{ Amplitude = 0.4, EasingMode = Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut };
                Animations.Transform.Translate(tbk_InCollection, 500, null, 0, 0, TimeSpan.FromMilliseconds(200), b, null);
                sum_album_gridview.ItemsSource = await Helpers.GetAlbums(_artist.Name);
            }
        }

        private void ParseBiography()
        {
            var strings = Helpers.LongTextParse(Helpers.PlainText(_artist.BioContent));

            foreach (string s in strings)
            {
                Color c = new Color();
                TextBlock t = new TextBlock
                {
                    FontSize = 14,
                    Foreground = new SolidColorBrush(new Color { R = 126, G = 126, B = 126, A=255 }),
                    TextWrapping = TextWrapping.Wrap
                };

                t.Text = s;
                BioPanel.Children.Add(t);
            }
        }
    }
}
